package com.sy.controller.message;

import com.sy.bean.message.*;
import com.sy.bean.message.excel.GoodsExcel;
import com.sy.bean.message.query.*;
import com.sy.common.bean.PageBean;
import com.sy.common.bean.ResultBean;
import com.sy.common.controller.BaseController;
import com.sy.service.message.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "message-商品管理")
@RequestMapping("/api")
public class GoodsController extends BaseController {
    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsCategoryService goodsCategoryService;

    @Autowired
    StockService stockService;

    @Autowired
    PropertyService propertyService;

    @Autowired
    ParameterService parameterService;

    @Autowired
    UnitService unitService;

    @PostMapping("/goods")
    @ApiOperation("商品列表")
    public ResultBean goodsQuery(@RequestBody GoodsQuery goodsQuery) {
        PageBean pageBean = goodsService.query(goodsQuery.toMap());
        return success("商品列表", pageBean);
    }

    @ApiOperation("商品类别树")
    @GetMapping("/category/tree")
    public ResultBean tree() {
        List list = goodsCategoryService.tree();
        return success("商品类别树", list);
    }

    @ApiOperation("新增类别")
    @PostMapping("/category")
    public ResultBean cate(@RequestBody Category goodsCategory) {
        int rs = goodsCategoryService.saveCategory(goodsCategory);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }

    @ApiOperation("商品新增")
    @PostMapping("/goods/save")
    public ResultBean saveGoods(@RequestBody Goods goods) {
        int rs = goodsService.save(goods);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }

    @ApiOperation("删除商品")
    @DeleteMapping("/goods/del")
    public ResultBean delGoods(int id) {
        int rs = goodsService.delById(id);
        return rs == 1 ? success("删除成功") : fail("删除失败");
    }

    @ApiOperation("编辑商品详情")
    @PutMapping("/goods/update")
    public ResultBean updateGoods(@RequestParam("id") int id, @RequestParam("goodsDetail") String goodsDetail) {
        int rs = goodsService.updateDetail(id, goodsDetail);
        return rs == 1 ? success("编辑成功") : fail("编辑失败");
    }

    @ApiOperation("仓库列表")
    @PostMapping("/stock/list")
    public ResultBean query(@RequestBody StockQuery stockQuery) {
        PageBean pageBean = stockService.query(stockQuery.toMap());
        return success("仓库列表", pageBean);
    }

    @ApiOperation("仓库新增")
    @PostMapping("/stock/save")
    public ResultBean saveStock(@RequestBody Stock stock) {
        int rs = stockService.save(stock);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }

    @ApiOperation("删除仓库")
    @DeleteMapping("/stock/del")
    public ResultBean delStock(int id) {
        int rs = stockService.delete(id);
        return rs == 1 ? success("删除成功") : fail("删除失败");
    }

    @ApiOperation("编辑仓库")
    @PutMapping("/stock/update")
    public ResultBean updateStock(@RequestBody Stock stock) {
        int rs = stockService.update(stock);
        return rs == 1 ? success("编辑成功") : fail("编辑失败");
    }

    @ApiOperation("属性列表")
    @PostMapping("/property/list")
    public ResultBean queryProperty(@RequestBody PropertyQuery propertyQuery) {
        PageBean pageBean = propertyService.query(propertyQuery.toMap());
        return success("属性列表", pageBean);
    }

    @ApiOperation("属性新增")
    @PostMapping("/property/save")
    public ResultBean saveProperty(@RequestBody Property property) {
        int rs = propertyService.save(property);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }

    @ApiOperation("删除属性")
    @DeleteMapping("/property/del")
    public ResultBean delProperty(int id) {
        int rs = propertyService.delete(id);
        return rs == 1 ? success("删除成功") : fail("删除失败");
    }

    @ApiOperation("编辑属性")
    @PutMapping("/property/update")
    public ResultBean updateProperty(@RequestBody Property property) {
        int rs = propertyService.update(property);
        return rs == 1 ? success("编辑成功") : fail("编辑失败");
    }

    @ApiOperation("参数列表")
    @PostMapping("/parameter/list")
    public ResultBean queryParameter(@RequestBody ParameterQuery parameterQuery) {
        PageBean pageBean = parameterService.query(parameterQuery.toMap());
        return success("参数列表", pageBean);
    }

    @ApiOperation("参数新增")
    @PostMapping("/parameter/save")
    public ResultBean saveParameter(@RequestBody Parameter parameter) {
        int rs = parameterService.save(parameter);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }

    @ApiOperation("删除参数")
    @DeleteMapping("/parameter/del")
    public ResultBean delParameter(int id) {
        int rs = parameterService.delete(id);
        return rs == 1 ? success("删除成功") : fail("删除失败");
    }

    @ApiOperation("编辑参数")
    @PutMapping("/parameter/update")
    public ResultBean updateParameter(@RequestBody Parameter parameter) {
        int rs = parameterService.update(parameter);
        return rs == 1 ? success("编辑成功") : fail("编辑失败");
    }

    @ApiOperation("单位列表")
    @PostMapping("/unit/list")
    public ResultBean queryUnit(@RequestBody UnitQuery unitQuery) {
        PageBean pageBean = unitService.query(unitQuery.toMap());
        return success("单位列表", pageBean);
    }

    @ApiOperation("单位新增")
    @PostMapping("/unit/save")
    public ResultBean saveUnit(@RequestBody Unit unit) {
        int rs = unitService.save(unit);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }

    @ApiOperation("删除单位")
    @DeleteMapping("/unit/del")
    public ResultBean delUnit(int id) {
        int rs = unitService.delete(id);
        return rs == 1 ? success("删除成功") : fail("删除失败");
    }

    @ApiOperation("编辑单位")
    @PutMapping("/unit/update")
    public ResultBean updateUnit(@RequestBody Unit unit) {
        int rs = unitService.update(unit);
        return rs == 1 ? success("编辑成功") : fail("编辑失败");
    }

    @PostMapping("/goods/exportGoods")
    @ApiOperation("导出商品列表")
    public ResultBean<GoodsExcel> exportGoods(@RequestBody Map map) {
        String url = goodsService.exportGoods(map);
        return success("导出商品列表", url);
    }
}
